package com.lier.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lier.gulimall.product.entity.BrandEntity;
import com.lier.gulimall.product.entity.CategoryEntity;
import com.lier.gulimall.product.service.BrandService;
import com.lier.gulimall.product.service.CategoryService;
import com.lier.gulimall.product.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lier.gulimall.product.entity.CategoryBrandRelationEntity;
import com.lier.gulimall.product.service.CategoryBrandRelationService;
import com.lier.common.utils.PageUtils;
import com.lier.common.utils.R;

import javax.annotation.Resource;


/**
 * 品牌分类关联
 *
 * @author lier
 * @email 664478853@qq.com
 * @date 2025-06-06 14:14:15
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Resource
    private BrandService brandService;

    @Resource
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("brands/list")
    public R listBrands(@RequestParam(value = "catId") Long catId){
        List<BrandEntity> resData = categoryBrandRelationService.getBrandsByCatId(catId);

        List<BrandVo> data = resData.stream().map(item -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(item.getBrandId());
            brandVo.setBrandName(item.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", data);
    }

    /**
     * 获取当前品牌的所有分类
     */
    @GetMapping("catelog/list")
    public R categoryList(@RequestParam("brandId") Long brandId){
        LambdaQueryWrapper<CategoryBrandRelationEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CategoryBrandRelationEntity::getBrandId, brandId);
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(wrapper);
        return R.ok().put("data", data);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){

        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        if(brandEntity != null){
            categoryBrandRelation.setBrandName(brandEntity.getName());
        }
        CategoryEntity category = categoryService.getById(categoryBrandRelation.getCatelogId());
        if(category != null){
            categoryBrandRelation.setCatelogName(category.getName());
        }
        categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
